package com.seednia.foundation.framework.dto;

import java.util.ArrayList;
import java.util.List;

public class JoinParams {
	
	private List<JoinTable> joinTables = new ArrayList<JoinTable>();
	
	public JoinParams()
	{}
	
	public List<JoinTable> getJoinTables() {
		return joinTables;
	}

	public void setJoinTables(List<JoinTable> joinTables) {
		this.joinTables = joinTables;
	}
	
	public JoinTable addJoinTable(String name, String alias)
	{
		JoinTable newJoinTable = new JoinTable(name, alias);
		this.joinTables.add(newJoinTable );
		return newJoinTable;
	}

	public class Column {
		private String name = null;
		private String alias = null;
		
		public Column(String name, String alias)
		{
			this.name = name;
			this.alias = alias;
		}
		
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getAlias() {
			return alias;
		}
		public void setAlias(String alias) {
			this.alias = alias;
		}
		
	}

	public class JoinTable {
		public static final String TYPE_INNER = "inner";
		public static final String TYPE_LEFT = "left";
		public static final String TYPE_OUTTER = "outter";
		
		private String name = null;
		private String alias = null;
		
		private String jointType = TYPE_LEFT; // left, outer, etc.
		private String onClause = ""; // left, outer, etc.
		
		public JoinTable(String name, String alias)
		{
			this.name = name;
			this.alias = alias;
		}
		
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getAlias() {
			return alias;
		}
		public void setAlias(String alias) {
			this.alias = alias;
		}
		
		
		public String getJointType() {
			return jointType;
		}
		public void setJointType(String jointType) {
			this.jointType = jointType;
		}

		public String getOnClause() {
			return onClause;
		}
		public void setOnClause(String onClause) {
			this.onClause = onClause;
		}
		/**
		 * Produces <alias>.<thisColumn> = <foreignColumn>
		 * Precondition: alias must have been set first
		 * @param thisCol
		 * @param foreignColumn
		 */
		public void addOnClause(String thisCol, String foreignColumn) {
			if (this.onClause.length() > 1)
				this.onClause += " AND ";
			this.onClause += this.getAlias()+"."+thisCol +" = "+ foreignColumn;
		}


		private List<Column> selectColumns = new ArrayList<Column>(); 
		
		public List<Column> getSelectColumns() {
			return selectColumns;
		}

		public void setSelectColumns(List<Column> selectColumns) {
			this.selectColumns = selectColumns;
		}
		
		public Column addColumn(String name, String alias)
		{
			Column newColumn  = new Column(name, alias);
			selectColumns.add(newColumn);
			return newColumn;
		}
		public Column addColumn(String name)
		{
			return addColumn(name, null);
		}
	}
}
